package com.calenda.setevent.fragment;

import java.util.ArrayList;

import com.calenda.setevent.R;
import com.calenda.setevent.adapter.GridGroupAdapter;
import com.calenda.setevent.api.BaseAjaxCallBack.IAjaxCallbackMMK;
import com.calenda.setevent.entity.GroupEntity;
 

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;
import static com.calenda.setevent.api.SetEventApi.*;

public class GridGroupFragment extends BaseFragment implements IAjaxCallbackMMK {

	private GridView gridView;
	private ArrayList<GroupEntity> groupEntities = new ArrayList<GroupEntity>();
	private GridGroupAdapter mGridGroupAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.lst_group_fragment, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		gridView = (GridView) view.findViewById(R.id.gridView);
		mGridGroupAdapter = new GridGroupAdapter(getActivity(), groupEntities);
		gridView.setAdapter(mGridGroupAdapter);
		getAllGroup(getActivity(), this);
	}

	@Override
	public <T> void handler(T t, int code) {
		// TODO Auto-generated method stub

		if (code == COMPLETE) {

			@SuppressWarnings("unchecked")
			ArrayList<GroupEntity> groupEntities = (ArrayList<GroupEntity>) t;

			if (!groupEntities.isEmpty()) {
				this.groupEntities.addAll(groupEntities);
				mGridGroupAdapter.notifyDataSetChanged();
			}

		} else if (code == ERROR) {
			String msg = (String) t;
			Toast.makeText(this.getActivity(), msg, Toast.LENGTH_LONG).show();
		}
	}
}
